package ru.osu.acm.testing.beans;

import ru.osu.acm.common.config.Config;
import ru.osu.acm.common.entities.TeamForShow;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * User: Integra
 * Date: 10.07.2009
 * Time: 17:55:34
 */
public class ShowTeamBean {
    @Resource(name = Config.jndi)
    private DataSource source;


    private TeamForShow[] teams;
    private HashMap<Integer, ArrayList<String>> hm = new HashMap<Integer, ArrayList<String>>();

    public String Get() {

        ArrayList<TeamForShow> com = new ArrayList<TeamForShow>();
        int cnt = 0;
        try {
            Connection conn = source.getConnection();
            try {
                PreparedStatement statement = conn.prepareStatement("select * from team order by id");
                ResultSet resultSet = statement.executeQuery();
                while (resultSet.next()) {
                    PreparedStatement statemnt_id_mem = conn.prepareStatement("select * from teamperson where team = ? and stopdate is null");
                    statemnt_id_mem.setInt(1, resultSet.getInt("id"));
                    ResultSet resultSet_id_mem = statemnt_id_mem.executeQuery();
                    ArrayList<String> mem = new ArrayList<String>();
                    int cnt_person = 0;
                    while (resultSet_id_mem.next()) {
                        PreparedStatement statement_person = conn.prepareStatement("select person.login, person.firstname, person.secondname from person where id= ? ");
                        statement_person.setInt(1, resultSet_id_mem.getInt("person"));
                        ResultSet res = statement_person.executeQuery();
                        cnt_person++;
                        while (res.next()) {
                            mem.add(res.getString("firstname") + " " + res.getString("secondname") + " ( " + res.getString("login") + " )");
                        }
                        statement_person.close();
                        res.close();
                    }
                    String[] convert_mem;
                    if (cnt_person < 3) {
                        convert_mem = new String[3];
                    }
                    else {
                        convert_mem = new String[cnt_person];
                    }
                    int i = 0;
                    for (String s : mem) {
                        convert_mem[i] = s;
                        i++;
                    }
                    if (cnt_person < 3) {
                        for (int j = i; j < 3; j++) {
                            convert_mem[j] = "none";
                        }
                    }
                    cnt++;
                    hm.put(resultSet.getInt("id"), mem);
                    com.add(new TeamForShow(
                            resultSet.getInt("id"),
                            resultSet.getString("teamname"),
                            cnt_person,
                            convert_mem,
                            resultSet.getString("login")
                    ));
                    statemnt_id_mem.close();
                    resultSet_id_mem.close();

                }
                resultSet.close();
                statement.close();
            }
            finally {
                conn.close();
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        teams = new TeamForShow[cnt];
        int count = 0;
        for (TeamForShow i : com) {
            teams[count++] = i;
        }
        return "null";
    }

    public TeamForShow[] getteams() {
        Get();
        return teams;
    }

    public void setteams(TeamForShow[] teams) {
        this.teams = teams;
    }


}